/**
 * 游戏管理
 */
class GameManager {
    private static _instance:GameManager;
    //当前的关卡
    public curCustoms:number = 1;
    //当前三发的次数
    public curThreeTimes:number = 0;
    //是否复活过
    public isRelive:boolean = false;
    //屠龙宝刀的当前的次数
    public curDragonTimes:number = 0;
    //临时存的使用的刀的id
    public useingKnifeId:number;
    //今天已经三发分享的次数
    public todayThreeShareTimes:number = 0;
    //今天已经看视频的次数
    public todayVedioTimes:number = 0;
    //今天已经屠龙宝刀已经分享的次数
    public todayDragonShareTimes:number = 0;
    //体验的刀的次数
    public experienceKnifeTimes:number = 0;
    //是否随机三刀
    public randomThreeKnife:boolean = false;
    //是否随机屠龙体验
    public randomDragon:boolean = false;

    //当前的小关卡
    public curCustomsIndex:number = 0;

    //当前是否是boss关卡
    public curCustomsIsBoss:boolean = false;

    //当前是否可以展示宝箱
    public isCanshowBox:boolean = false;

    //是否是随机体验的三发
    public isRandomThreeFlag:boolean = false;

    //是否可以弹出幸运武器
    public isCanShowLuckyWeapon = null;
    //是否是通过分享去获得幸运武器
    public isGetLuckyWeaponByShare = false;

    //一刀切的数量
    public one_cut_num:number = 0;
    constructor()
    {
        this.init();
        this.curCustoms = RoleCache.instance().curCustoms;
    }

    public addTodayVedioTimes()
    {
        GameManager.instance.todayVedioTimes++;
        let key_da = {
            key:'vedio_times',
            value:GameManager.instance.todayVedioTimes
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public clearVedioTimes()
    {
        GameManager.instance.todayVedioTimes = 0;
        let key_da = {
            key:'vedio_times',
            value:0
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public addTodayDragonShareTime()
    {
        GameManager.instance.todayDragonShareTimes++;
        let key_da = {
            key:'dragon_share_times',
            value:GameManager.instance.todayDragonShareTimes
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public clearDragonShareTimes()
    {
        GameManager.instance.todayDragonShareTimes = 0;
        let key_da = {
            key:'dragon_share_times',
            value:0
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public addTodayThreeShareTimes()
    {
        GameManager.instance.todayThreeShareTimes++;
        let key_da = {
            key:'three_share_times',
            value:GameManager.instance.todayThreeShareTimes
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public clearThreeShareTimes()
    {
        GameManager.instance.todayThreeShareTimes = 0;
        let key_da = {
            key:'three_share_times',
            value:0
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public init()
    {
        this.curThreeTimes = WeChatgGameManager.instance().getStorge2("three_times");
        this.curDragonTimes = WeChatgGameManager.instance().getStorge2("dragon_times");
        this.todayThreeShareTimes = WeChatgGameManager.instance().getStorge2("three_share_times");

        this.todayVedioTimes = WeChatgGameManager.instance().getStorge2("vedio_times");
        this.isRelive = false;
        this.curCustomsIndex = 1;
    }

    public showExperienceThree()
    {
        let self = this;
        let sureCall:Function;
        if(this.todayVedioTimes >= GameConst.MaxVedioCnt)
        {
            sureCall = function()
            {
                SharePrase.instance().requestOneAdvertisement({type:ShareId.ThreeKnife});
            }
        }
        else
        {
            sureCall = function()
            {
                let Success_call = function()
                {  
                    ResourceUtil.instance().addResource('a_4_'+GameConst.GameConst.vedio_three_count);
                };
                let Error_call = function()
                {  
                    MessageUtil.tips('观看完整视频才能获取更多体验哦！');
                };
                WeChatgGameManager.instance().showVideoAd(GameConst.adList.other_vedio, Success_call, Error_call, self);
            }
        }
        
        let data = {
            tips:'体验结束，点击继续使用三箭齐发！',
            iconUrl:'icon_three_png',
            sureCall:sureCall,
            isPop:true,
            effect:1,
            scope:self
        }
        GameUtil.openConfirmUI(data);
    }

    public reduceCurThreeTimes()
    {
        GameManager.instance.curThreeTimes--;
        let key_da = {
            key:'three_times',
            value:GameManager.instance.curThreeTimes
        }
        //当且仅当是随机三发的三箭齐发，才弹出继续使用的弹出框
        if(GameManager.instance.curThreeTimes <= 0 && this.isRandomThreeFlag)
        {
            this.isRandomThreeFlag = false;
            GameManager.instance.showExperienceThree();
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public reduceCurDragonTimes()
    {
        GameManager.instance.curDragonTimes--;
        let key_da = {
            key:'dragon_times',
            value:GameManager.instance.curDragonTimes
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
    }

    public addCurThreeTimes(times)
    {
        GameManager.instance.curThreeTimes += times;
        let key_da = {
            key:'three_times',
            value:GameManager.instance.curThreeTimes
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
        let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
        if(logic)
        {
            logic.updateThree();
            logic.updateThreeGroup(true);
        }
    }

    public addCurDragonTimes(times)
    {
        GameManager.instance.curDragonTimes += times;
        let key_da = {
            key:'dragon_times',
            value:GameManager.instance.curDragonTimes
        }
        WeChatgGameManager.instance().setStorage(key_da, null);
        let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
        if(logic)
        {
            logic.updateDragonGroup(true);
        }
        
    }

    public static get instance():GameManager
    {
        if(!this._instance)
        {
            this._instance = new GameManager();
        }
        return this._instance;
    }

    //获取当前关卡可以击空的次数
    public getCurCustomsHitLostNum()
    {
        if(this.curCustoms <= 20)
            return 1;
        else
        {
            let curWeaponId = RoleCache.instance().curWeaponId;
            let weaponData = WeaponTpl.instance().getWeaponDataByWeaponId(curWeaponId);
            if(weaponData)
            {
                let cnt = weaponData.life;
                return cnt;
            }
            else
            {
                return 0;
            }
        }
    }

    //判断道具是否有三发
    public judgeIsHaveThree()
    {
        let curWeaponId = RoleCache.instance().curWeaponId;
        let weaponData = WeaponTpl.instance().getWeaponDataByWeaponId(curWeaponId);
        if(weaponData)
        {
            let property = weaponData.property;
            if(WeaponPropertyType.threeKnife == property)
            {
                let Probability = weaponData.three_shot_probability;
                if(Probability)
                {
                    return Math.random() <= (Probability/100);
                }
            }
        }
        else
        {
            return false;
        }
    }

    //检测是否有奖励
    /**
     * @param not_check 不用检测
     */
    public checkIsHaveAward(not_check:boolean = false)
    {
        let curCustoms = GameManager.instance.curCustoms;
        this.curCustomsIsBoss = false;
        //优先检测奖励关卡 其次是果篮奖励 其次是大转盘奖励
        // if(curCustoms % DataJson.const_define.task_customs.award == 0)
        // {
        //     //任务关卡
        //     UIManager.instance().openUILogic(TaskCustomsUI);
        // }
        // else if(curCustoms % DataJson.const_define.luck_customs.award == 0)
        // {
        //     //体验关卡
        //     UIManager.instance().openUILogic(AwardCustomsUI);
        // }
        if(this.curThreeTimes <= 0 && this.judgeIsHaveThree())
        {
            //没有三发的状态且可以获得三发
            this.curThreeTimes++;  
            let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
            if(logic)
            {
                MessageUtil.tips('三箭齐发效果触发！');
                logic.updateThree();
                logic.updateThreeGroup(true);
            }
        }
        if(this.isCanShowLuckyWeapon )
        {
            if(not_check)return;
            //弹出送武器
            let data = {
                isPop:true,
                effect:1
            }
            UIManager.instance().openUILogic(LuckWeaponUI, data);
        }
        else if(curCustoms % DataJson.const_define.luck_fruitBasket.award == 0)
        {
            if(not_check)return;
            let data = {
                isPop:true,
                effect:1
            }
            UIManager.instance().openUILogic(FruitBasketUI, data);
        }
        else if(curCustoms % DataJson.const_define.luck_wheel.award == 0)
        {
            if(not_check)return;
            let data = {
                isPop:true,
                effect:5
            }
            UIManager.instance().openUILogic(AwardUI,data);
        }
        else if(curCustoms % DataJson.const_define.Boss_customs.award == 0)
        {
            this.curCustomsIsBoss = true;
            //boss关卡
            let data = {
                isPop:true,
                effect:1,
                tips_id:'tips_1'
            }
            UIManager.instance().openUILogic(SmallConfirmUI,data);
        }
        // else if(curCustoms % DataJson.const_define.three_experience.award == 0)
        // {
        //     let call = function()
        //     {
        //         let logic:GameUI = UIManager.instance().getLogicByName(UIConst.GAME_UI) as GameUI;
        //         if(logic)
        //         {
        //             logic.updateThree();
        //             logic.updateThreeGroup();
        //         }
        //     }
        //     let scope = this;
        //     egret.setTimeout(()=>{
        //         let award = 'a_4_3';
        //         GameUtil.openAwardUI(award, call, scope);
        //     },this,300);
        // }
        else if((curCustoms % DataJson.const_define.random_three.award == 0) || this.curCustoms == 6)
        {
            //随机三发
           this.randomThreeKnife = true;
           this.isRandomThreeFlag = true;
        }
        else if(curCustoms % DataJson.const_define.Dragon_Sword_experience.award == 0 || this.curCustoms == 15)
        {
            //屠龙随机
            this.randomDragon = true;
        }
        else if(Math.random() > 0.85 && (this.curThreeTimes <= 0 && this.curCustoms > 20) ||  this.curCustoms == 2)
        {
            //可以展示宝箱
            this.isCanshowBox = true;
            let data = {
                isPop:true,
                effect:1,
                tips_id:'tips_2'
            }
            UIManager.instance().openUILogic(SmallConfirmUI,data);
        }
    }
}

